<?php
/**
 * IG_Cryptoguard
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Cryptoguard.php 806 2011-08-31 10:13:43Z mmxie $
 */
class IG_Cryptoguard
{
    protected static $_instance;
    /**
     *
     * @param string $adapter
     * @param string|array $config
     * @return IG_Cryptoguard_Abstract
     * @throws IG_Cryptoguard_Exception
     */
    public static function factory($adapter, $config = null)
    {
        $adapterName = 'IG_Cryptoguard_' . ucfirst($adapter);

        if (!class_exists($adapterName)) {
            throw new IG_Task_Exception("class \"$adapterName\" not exists.");
        }

        $cryptoguardAdapter = new $adapterName($config);

        if (! $cryptoguardAdapter instanceof IG_Cryptoguard_Abstract) {
            throw new IG_Cryptoguard_Exception("Adapter class '$adapterName' does not extend IG_Cryptoguard_Abstract");
        }

        return $cryptoguardAdapter;
    }

    public static function isEnabled()
    {
        $options = self::_getOptions();
        return !empty($options['enable']);
    }

    /**
     * Cryptoguard instance
     * @return IG_Cryptoguard_Abstract
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            $options = self::_getOptions();
            self::$_instance = self::factory($options['adapter'], $options['params']);
        }

        return self::$_instance;
    }


    protected static function _getOptions()
    {
        $front = Zend_Controller_Front::getInstance();
        return $front->getParam('bootstrap')->getOption('cryptoguard');
    }
}